package action.common;

import org.apache.struts2.ServletActionContext;

import com.octo.captcha.service.CaptchaService;
import common.impl.VerifyCode;
import common.interf.IServletHandle;
//import com.octo.captcha.service.captchastore.FastHashMapCaptchaStore;

public class VerifyCodeAction {
	private String mobile;
	private CaptchaService captchaService;
	private IServletHandle servletHandle;
	
	private String inputCode;
	
	public String getInputCode() {
		return inputCode;
	}

	public void setInputCode(String inputCode) {
		this.inputCode = inputCode;
	}

	public String getMobile() {
		return mobile;
	}

	public void setMobile(String mobile) {
		this.mobile = mobile;
	}

	public void setCaptchaService(CaptchaService captchaService) {
		this.captchaService = captchaService;
	}

	public void setServletHandle(IServletHandle servletHandle) {
		this.servletHandle = servletHandle;
	}

	public void graphCode(){
		VerifyCode vc = VerifyCode.getInstance();
		vc.generateCode(ServletActionContext.getRequest(), ServletActionContext.getResponse());
	}
	
	public void getSmsCode() throws Exception{
		VerifyCode vc = VerifyCode.getInstance();
		String code = vc.getRandString(6);
		String msg = "WiFi密码为:" + code + ",30分钟内输入有效";
//		SmsHandle.sendSMS(mobile, msg);
//		int tryTime=0;
//		//失败发三次
//		while(SmsHandle.sendSMS(mobile, msg)<0 && tryTime<3){
//			Thread.sleep(5000);
//			tryTime++;
//		}
		
		vc.generateCode(ServletActionContext.getRequest(),code);
	}
	
	public void verifyCode(){
		String str = "";
		String sessionCode = (String)ServletActionContext.getRequest().getSession().getAttribute("verifyCode");
		if(sessionCode.equals(inputCode)){
			str = "1";
		}
		else{
			str = "0";
		}
		servletHandle.writeToClient1(ServletActionContext.getResponse(), str);
	}
}
